﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.JointVariableLimitsEditor.CUiJVLEditorForm
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using C1.Win.C1FlexGrid;
using C1.Win.C1FlexGrid.Util.BaseControls;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui;
using UiInternalControls.WindowsFormsControls.GraphEnums;


namespace DnProcessSimulateCommands.JointVariableLimitsEditor;

internal class CUiJVLEditorForm : TxForm
{
  private CApJVLEditorViewModel _viewModel;
  private int _suspendEnablersRefreshCounter;
  private ToolTip _tt;
  private object _varLimitsParams;
  private IContainer components;
  private Button _okButton;
  private Button _cancelButton;
  private SplitContainer _splitContainer;
  private CUiJVLEditorGraph _graph;
  private TxButton _settingsButton;
  private TxButton _moveDownButton;
  private TxButton _moveUpButton;
  private TxButton _deleteButton;
  private CUiJVLEditorGrid _grid;

  public CUiJVLEditorForm()
  {
    this.InitializeComponent();
    this._moveDownButton.SetImage(TxImageManager.GetImageByKey("ArrowDown"), new Size());
    this._moveUpButton.SetImage(TxImageManager.GetImageByKey("ArrowUp"), new Size());
    this._settingsButton.SetImage(TxImageManager.GetImageByKey("Settings"), new Size());
    this._deleteButton.SetImage(TxImageManager.GetImageByKey("Delete"), new Size());
  }

  protected virtual void OnFormClosing(FormClosingEventArgs e)
  {
    e.Cancel = false;
    if (((Form) this).DialogResult == DialogResult.OK)
      this._viewModel.IO_Save();
    this.updateSplitterSettings();
    this._viewModel.UpdateGridSettings();
    this._viewModel.StoreSettings((Form) this);
    this.unregisterToGridEvents();
    // ISSUE: explicit non-virtual call
    __nonvirtual (((Form) this).OnFormClosing(e));
  }

  public virtual void OnInitTxForm()
  {
    base.OnInitTxForm();
    this._viewModel = new CApJVLEditorViewModel(this._varLimitsParams);
    ((Control) this).Text = string.Format(DnProcessSimulateCommands.JointVariableLimitsEditor.Resources.StringTable.EDITOR_DLG_CAPTION, (object) this._viewModel.IO_YName, (object) this._viewModel.IO_XName);
    this._viewModel.LoadSettings((Form) this);
    this.applySplitterSettings();
    this._grid.ViewModel = this._viewModel;
    this._graph.ViewModel = this._viewModel;
    this.setTooltips();
    this.refreshEnablers();
    this.registerToGridEvents();
  }

  internal object VarLimitsParams
  {
    get => this._varLimitsParams;
    set => this._varLimitsParams = value;
  }

  private void setTooltips()
  {
    if (this._tt == null)
      this._tt = new ToolTip();
    this._tt.SetToolTip((Control) this._settingsButton, DnProcessSimulateCommands.JointVariableLimitsEditor.Resources.StringTable.TT_SETTINGS_BUTTON);
    this._tt.SetToolTip((Control) this._deleteButton, DnProcessSimulateCommands.JointVariableLimitsEditor.Resources.StringTable.TT_DELETE_BUTTON);
    this._tt.SetToolTip((Control) this._moveUpButton, DnProcessSimulateCommands.JointVariableLimitsEditor.Resources.StringTable.TT_MOVE_UP_BUTTON);
    this._tt.SetToolTip((Control) this._moveDownButton, DnProcessSimulateCommands.JointVariableLimitsEditor.Resources.StringTable.TT_MOVE_DOWN_BUTTON);
  }

  private void applySplitterSettings()
  {
    CUiJVLEditorViewModelSettings settings = this._viewModel.Settings;
    int num1 = this._splitContainer.Panel1MinSize + 1;
    int num2 = this._splitContainer.Width - this._splitContainer.Panel2MinSize - this._splitContainer.SplitterWidth - 1;
    int num3 = (int) (this._viewModel.Settings.SplitterPosRatio * (double) this._splitContainer.Width);
    if (num3 < num1)
      num3 = num1;
    else if (num3 > num2)
      num3 = num2;
    try
    {
      this._splitContainer.SplitterDistance = num3;
    }
    catch (Exception ex)
    {
    }
  }

  private void updateSplitterSettings()
  {
    this._viewModel.Settings.SplitterPosRatio = (double) this._splitContainer.SplitterDistance / (double) this._splitContainer.Width;
  }

  private void registerToGridEvents()
  {
    this._grid.SelectionChanged += new EventHandler(this._grid_SelectionChanged);
    this._grid.CanDoActionChanged += new EventHandler(this._grid_CanDoActionChanged);
    this._grid.DataUpdated += new EventHandler(this._grid_DataUpdated);
  }

  private void unregisterToGridEvents()
  {
    this._grid.SelectionChanged -= new EventHandler(this._grid_SelectionChanged);
    this._grid.CanDoActionChanged -= new EventHandler(this._grid_CanDoActionChanged);
    this._grid.DataUpdated -= new EventHandler(this._grid_DataUpdated);
  }

  private void suspendEnablersRefresh() => ++this._suspendEnablersRefreshCounter;

  private void resumeEnablersRefresh()
  {
    if (this._suspendEnablersRefreshCounter <= 0)
      return;
    --this._suspendEnablersRefreshCounter;
    if (this._suspendEnablersRefreshCounter != 0)
      return;
    this.refreshEnablers();
  }

  private bool isEnablersRefreshSuspended => this._suspendEnablersRefreshCounter > 0;

  private void refreshEnablers()
  {
    if (this.isEnablersRefreshSuspended)
      return;
    this.refreshEnablerOKButton();
    this.refreshEnablerDeleteButton();
    this.refreshEnablerMoveUpButton();
    this.refreshEnablerMoveDownButton();
  }

  private void refreshEnablerOKButton()
  {
    if (this.isEnablersRefreshSuspended)
      return;
    bool flag = true;
    if (!this._grid.IsValid || this._viewModel.Count == 0)
      flag = false;
    this._okButton.Enabled = flag;
  }

  private void refreshEnablerDeleteButton()
  {
    if (this.isEnablersRefreshSuspended)
      return;
    bool flag = false;
    if (this._grid.CanDeleteActiveSelection)
      flag = true;
    ((Control) this._deleteButton).Enabled = flag;
  }

  private void refreshEnablerMoveUpButton()
  {
    if (this.isEnablersRefreshSuspended)
      return;
    bool flag = false;
    if (this._grid.CanMoveItemsUp)
      flag = true;
    ((Control) this._moveUpButton).Enabled = flag;
  }

  private void refreshEnablerMoveDownButton()
  {
    if (this.isEnablersRefreshSuspended)
      return;
    bool flag = false;
    if (this._grid.CanMoveItemsDown)
      flag = true;
    ((Control) this._moveDownButton).Enabled = flag;
  }

  private void _okButton_Click(object sender, EventArgs e)
  {
    if (((C1FlexGridBase) this._grid).Editor != null)
      ((C1FlexGridBase) this._grid).FinishEditing(true);
    ((Form) this).DialogResult = DialogResult.OK;
    ((Form) this).Close();
  }

  private void _cancelButton_Click(object sender, EventArgs e)
  {
    if (((C1FlexGridBase) this._grid).Editor != null)
      ((C1FlexGridBase) this._grid).FinishEditing(true);
    ((Form) this).DialogResult = DialogResult.Cancel;
    ((Form) this).Close();
  }

  private void _settingsButton_Click(object sender, EventArgs e)
  {
    CUiJVLEditorSettingsForm editorSettingsForm = new CUiJVLEditorSettingsForm(this._viewModel.Settings.Graph);
    if (((Form) editorSettingsForm).ShowDialog() != DialogResult.OK)
      return;
    this._viewModel.Settings.Graph = new CUiJVLEditorViewModelSettings.GraphSettings(editorSettingsForm.GraphSettings);
    this._viewModel.ApplyGraphSettings();
  }

  private void _deleteButton_Click(object sender, EventArgs e)
  {
    this._grid.DeleteActiveSelection();
  }

  private void _moveUpButton_Click(object sender, EventArgs e) => this._grid.MoveItemsUp();

  private void _moveDownButton_Click(object sender, EventArgs e) => this._grid.MoveItemsDown();

  private void _grid_SelectionChanged(object sender, EventArgs e) => this.refreshEnablers();

  private void _grid_CanDoActionChanged(object sender, EventArgs e) => this.refreshEnablers();

  private void _grid_DataUpdated(object sender, EventArgs e) => this.refreshEnablers();

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    this.components = (IContainer) new Container();
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiJVLEditorForm));
    this._splitContainer = new SplitContainer();
    this._grid = new CUiJVLEditorGrid(this.components);
    this._moveDownButton = new TxButton();
    this._moveUpButton = new TxButton();
    this._deleteButton = new TxButton();
    this._settingsButton = new TxButton();
    this._graph = new CUiJVLEditorGraph(this.components);
    this._okButton = new Button();
    this._cancelButton = new Button();
    this._splitContainer.BeginInit();
    this._splitContainer.Panel1.SuspendLayout();
    this._splitContainer.Panel2.SuspendLayout();
    this._splitContainer.SuspendLayout();
    ((ISupportInitialize) this._grid).BeginInit();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this._splitContainer, "_splitContainer");
    this._splitContainer.BackColor = SystemColors.ActiveBorder;
    this._splitContainer.BorderStyle = BorderStyle.FixedSingle;
    this._splitContainer.Name = "_splitContainer";
    this._splitContainer.Panel1.BackColor = SystemColors.Control;
    this._splitContainer.Panel1.Controls.Add((Control) this._grid);
    this._splitContainer.Panel1.Controls.Add((Control) this._moveDownButton);
    this._splitContainer.Panel1.Controls.Add((Control) this._moveUpButton);
    this._splitContainer.Panel1.Controls.Add((Control) this._deleteButton);
    this._splitContainer.Panel1.Controls.Add((Control) this._settingsButton);
    componentResourceManager.ApplyResources((object) this._splitContainer.Panel1, "_splitContainer.Panel1");
    this._splitContainer.Panel2.BackColor = SystemColors.Window;
    this._splitContainer.Panel2.Controls.Add((Control) this._graph);
    componentResourceManager.ApplyResources((object) this._splitContainer.Panel2, "_splitContainer.Panel2");
    componentResourceManager.ApplyResources((object) this._grid, "_grid");
    ((ScrollableControl) this._grid).BorderStyle = (BorderStyleEnum) 3;
    ((Control) this._grid).CausesValidation = false;
    ((C1FlexGridBase) this._grid).EditOptions = (EditFlags) 119;
    ((C1FlexGridBase) this._grid).KeyActionEnter = (KeyActionEnum) 0;
    ((C1FlexGridBase) this._grid).KeyActionTab = (KeyActionEnum) 3;
    ((Control) this._grid).Name = "_grid";
    ((RowColCollection) ((C1FlexGridBase) this._grid).Rows).Count = 13;
    ((C1FlexGridBase) this._grid).SelectionMode = (SelectionModeEnum) 7;
    componentResourceManager.ApplyResources((object) this._moveDownButton, "_moveDownButton");
    ((Control) this._moveDownButton).Name = "_moveDownButton";
    ((ButtonBase) this._moveDownButton).UseVisualStyleBackColor = true;
    ((Control) this._moveDownButton).Click += new EventHandler(this._moveDownButton_Click);
    componentResourceManager.ApplyResources((object) this._moveUpButton, "_moveUpButton");
    ((Control) this._moveUpButton).Name = "_moveUpButton";
    ((ButtonBase) this._moveUpButton).UseVisualStyleBackColor = true;
    ((Control) this._moveUpButton).Click += new EventHandler(this._moveUpButton_Click);
    componentResourceManager.ApplyResources((object) this._deleteButton, "_deleteButton");
    ((Control) this._deleteButton).Name = "_deleteButton";
    ((ButtonBase) this._deleteButton).UseVisualStyleBackColor = true;
    ((Control) this._deleteButton).Click += new EventHandler(this._deleteButton_Click);
    componentResourceManager.ApplyResources((object) this._settingsButton, "_settingsButton");
    ((Control) this._settingsButton).Name = "_settingsButton";
    ((ButtonBase) this._settingsButton).UseVisualStyleBackColor = true;
    ((Control) this._settingsButton).Click += new EventHandler(this._settingsButton_Click);
    this._graph.AllowZoom = false;
    componentResourceManager.ApplyResources((object) this._graph, "_graph");
    ((Control) this._graph).BackColor = Color.FromArgb((int) byte.MaxValue, (int) byte.MaxValue, (int) byte.MaxValue);
    this._graph.DataHotSpot = false;
    this._graph.DataLayerPointsCapacity = 30;
    ((Control) this._graph).ForeColor = Color.FromArgb(0, 0, 0);
    this._graph.MimimumPointSize = (MinimumPointSizeType) 2;
    ((Control) this._graph).Name = "_graph";
    this._graph.PointSize = (PointSizeType) 2;
    this._graph.SubTitle = (string) null;
    this._graph.Title = (string) null;
    this._graph.Xnull = 3.4028234663852886E+38;
    this._graph.Ynull = 3.4028234663852886E+38;
    componentResourceManager.ApplyResources((object) this._okButton, "_okButton");
    this._okButton.Name = "_okButton";
    this._okButton.UseVisualStyleBackColor = true;
    this._okButton.Click += new EventHandler(this._okButton_Click);
    componentResourceManager.ApplyResources((object) this._cancelButton, "_cancelButton");
    this._cancelButton.DialogResult = DialogResult.Cancel;
    this._cancelButton.Name = "_cancelButton";
    this._cancelButton.UseVisualStyleBackColor = true;
    this._cancelButton.Click += new EventHandler(this._cancelButton_Click);
    ((Form) this).AcceptButton = (IButtonControl) this._okButton;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Form) this).CancelButton = (IButtonControl) this._cancelButton;
    ((Control) this).Controls.Add((Control) this._splitContainer);
    ((Control) this).Controls.Add((Control) this._cancelButton);
    ((Control) this).Controls.Add((Control) this._okButton);
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiJVLEditorForm);
    this.SemiModal = false;
    this.ShouldCloseOnDocumentUnloading = true;
    ((Form) this).ShowIcon = false;
    this._splitContainer.Panel1.ResumeLayout(false);
    this._splitContainer.Panel2.ResumeLayout(false);
    this._splitContainer.EndInit();
    this._splitContainer.ResumeLayout(false);
    ((ISupportInitialize) this._grid).EndInit();
    ((Control) this).ResumeLayout(false);
  }
}
